package com.study.d012Components.treeview;


import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeItem.TreeModificationEvent;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.control.cell.ChoiceBoxTreeCell;
import javafx.scene.control.cell.ComboBoxTreeCell;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/** TreeView 自定义TableCell ,使用复杂的类型
*@description
*@author xtayhame
*date 2020年3月15日
*version
*since v1.0
*/
public class TreeViewMain2 extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane an = new AnchorPane();
		Button b1 = new Button("点击");

		TreeView<Organ> treeView = new TreeView<Organ>();
		treeView.setPrefSize(200, 300);

		TreeItem<Organ> root = new TreeItem<Organ>(new Organ("中国", true) );

		TreeItem<Organ> item_gd = new TreeItem<Organ>(new Organ("广东省", true));
		TreeItem<Organ> item_gd_1 = new TreeItem<Organ>(new Organ("广州市", false));
		TreeItem<Organ> item_gd_2 = new TreeItem<Organ>(new Organ("深圳市", true));
		item_gd.getChildren().addAll(item_gd_1,item_gd_2);


		TreeItem<Organ> item_hn = new TreeItem<Organ>(new Organ("河南省", true));
		TreeItem<Organ> item_hn_1 = new TreeItem<Organ>(new Organ("南阳市", false));
		TreeItem<Organ> item_hn_2 = new TreeItem<Organ>(new Organ("商丘市", true));
		item_hn.getChildren().addAll(item_hn_1,item_hn_2);

		root.getChildren().addAll(item_gd,item_hn);

		treeView.setRoot(root);


		an.getChildren().addAll(b1,treeView);
		AnchorPane.setTopAnchor(treeView, 30.0);
		AnchorPane.setLeftAnchor(treeView, 50.0);
		stage.setScene(new Scene(an, 400, 500));
		stage.show();


		treeView.setEditable(true);

		//使用TextFieldTreeCell 进行显示数据转换
	/*	treeView.setCellFactory(TextFieldTreeCell.forTreeView(new StringConverter<Organ>() {

			@Override
			public String toString(Organ object) {
				// TODO Auto-generated method stub
				return object.getName();
			}

			@Override
			public Organ fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		}));*/



		//第二种使用ComboBoxTreeCell
		/*treeView.setCellFactory(ComboBoxTreeCell.forTreeView(new StringConverter<Organ>() {

			@Override
			public String toString(Organ object) {
				// TODO Auto-generated method stub
				return object.getName();
			}

			@Override
			public Organ fromString(String string) {
				Organ organ = new Organ(string, false);
				return organ;
			}
		}, new Organ("南沙", false),new Organ("番禺", false)));*/


		//第三种：使用CheckBoxTreeCell
		/*treeView.setCellFactory(CheckBoxTreeCell.forTreeView(new Callback<TreeItem<Organ>, ObservableValue<Boolean>>() {
			@Override
			public ObservableValue<Boolean> call(TreeItem<Organ> param) {
				// TODO Auto-generated method stub
				if(treeView.getTreeItemLevel(param) ==1){
					//如果是第一层节点，则全部设置为选中
					param.getValue().setIsCheck(true);

				}else{
					param.getValue().setIsCheck(false);
				}

				return param.getValue().getIsCheckProperty();
			}
		}, new StringConverter<TreeItem<Organ>>() {

			@Override
			public String toString(TreeItem<Organ> object) {
				// TODO Auto-generated method stub
				return object.getValue().getName();
			}

			@Override
			public TreeItem<Organ> fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		}));
*/



		//第四种：使用ChoiceBoxTreeCell
		treeView.setCellFactory(ChoiceBoxTreeCell.forTreeView(new StringConverter<Organ>() {

			@Override
			public String toString(Organ object) {
				// TODO Auto-generated method stub
				return object.getName();
			}

			@Override
			public Organ fromString(String string) {
				// TODO Auto-generated method stub
				return new Organ(string, false);
			}
		}, new Organ("南沙", false),new Organ("番禺", false)));


		//监听当前选择的节点
		treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<Organ>>() {

			@Override
			public void changed(ObservableValue<? extends TreeItem<Organ>> observable, TreeItem<Organ> oldValue,
					TreeItem<Organ> newValue) {
				if(newValue !=null){
					System.out.println("当前值：" + newValue.getValue().getName()  +"       是否选中："+newValue.getValue().getIsCheck());
				}
			}
		});

	}
}
